package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.service.AssignCustomerService;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.List;

public class AssignCustomerServiceImpl
  implements AssignCustomerService
{
  private CustomerDAO customerDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private SaleStageSetDAO saleStageSetDAO;

  public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public List getPageAssignCustomers(String customerName, Integer currentUserId, Page page)
  {
    if (currentUserId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("7");
      customer.setDeptIds2(deptIds);
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);

        SaleStageSet saleStage = new SaleStageSet();
        saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
        if (saleStage != null) {
          cCustomer.setSaleStage(saleStage.getStageName());
        }
      }
    }
    return customers;
  }

  public Integer getPageAssignCustomersCount(String customerName, Integer currentUserId)
  {
    if (currentUserId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    if (!StringUtils.isEmpty(deptIds)) {
      customer.setCustomerCategory("7");
      customer.setDeptIds2(deptIds);
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }
}